package com.duyi.demo.component;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ctx
 * @date 2020/2/16
 * @description
 */
//拦截器
@Component
public class MyInterceptor implements HandlerInterceptor {


    public boolean prehandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler)throws Exception{
        //接受session
        Object user = request.getSession().getAttribute("loginUser");
        if(user!=null){
            //判断当前是否登录

            return true;
        }
        //如果未登录 跳转登录页
        request.getRequestDispatcher("/login").forward(request,response);
        return false;

    }

}
